package com.zhang.web.servlet;


import com.fasterxml.jackson.databind.util.BeanUtil;
import com.zhang.pojo.User;
import com.zhang.service.UserService;
import com.zhang.service.impl.UserServiceImpl;
import org.springframework.beans.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/regiestServlet")
public class RegiestServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //1.获取数据封装为模型
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String sex = req.getParameter("sex");
        String[] loves = req.getParameterValues("love");
        List<Integer> ids = new ArrayList<>();
        for (String love : loves) {
            Integer i = Integer.parseInt(love);
            ids.add(i);
        }
        User user = new User(0,username,password,email,sex,ids);



        //2.保存用户数据
        userService.regiest(user);

        //3.注册成功后跳转到登录页面
        resp.sendRedirect("/userpro/login.html");

    }
}






















